<template>
    <div class="main-form">
        <div class="login-main">
            <div class="login-top">
                <span>博客后台管理登录</span>
                <span class="bg1"></span>
                <span class="bg2"></span>
            </div>
            <div class="login-form">
                <el-form ref="form" :model="state.form" label-width="90px">
                    <el-form-item label="登陆帐号">
                        <el-input v-model="state.form.account"></el-input>
                    </el-form-item>
                    <el-form-item label="登陆密码">
                        <el-input v-model="state.form.password" type="password"></el-input>
                    </el-form-item>
                </el-form>
                <div class="login-btn">
                    <el-button type="primary" round @click="onSubmit">登陆</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>

import { nextTick, reactive, ref, toRef } from 'vue'

import { request } from "@/utils/request";
import md5 from 'js-md5';
import store from '@/store';

import { initRouter } from '@/router/routerUtil'
import { openPage } from '@/utils/tabs.js';
import { ElMessage } from 'element-plus'


let state = reactive({
    form: {
        account: 'admin',
        password: '123456'
    },
    token: ""
})

let onSubmit = () => {
    request({
        url: '/admin/login',
        method: 'post',
        data: {
            account: state.form.account,
            password: md5(state.form.password)
        }
    }).then(function (response) {
        let data = response.data;
        if(response.code == 200) {
            ElMessage.success({
                message: response.msg,
                type: 'success',
            })
            store.dispatch('user/setLoginInfo', data);
            initRouter(()=>{
                openPage('/home');
            });
        }else {
            ElMessage.error(response.msg)
        }
    }).catch(function (error) {
        console.log('登陆失败', error);
    });
}
</script>

<style scoped>
    @import url('@/styles/login.css')
</style>
